package com.atguigu.cart.controller;

import com.atguigu.cart.service.CartService;
import com.atguigu.cart.vo.CartItemVo;
import com.atguigu.cart.vo.CartVo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class CartController {
  @Resource
  private CartService cartService;

  @RequestMapping("/cart.html")
  public String getCartList(Model model) {
    CartVo cartVo=cartService.getCart();
    model.addAttribute("cart", cartVo);
    return "cartList";
  }

  @RequestMapping("/success.html")
  public String success() {
    return "success";
  }

  /**
   * 添加商品到购物车
   * RedirectAttributes.addFlashAttribute():将数据放在session中，可以在页面中取出，但是只能取一次
   * RedirectAttributes.addAttribute():将数据放在url后面
   * @return
   */
  @RequestMapping("/addCartItem")
  public String addCartItem(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num, RedirectAttributes attributes) {
    cartService.addCartItem(skuId, num);
    attributes.addAttribute("skuId", skuId);
    return "redirect:http://cart.gulimall.com/addCartItemSuccess";
  }

  @RequestMapping("/addCartItemSuccess")
  public String addCartItemSuccess(@RequestParam("skuId") Long skuId,Model model) {
    CartItemVo cartItemVo = cartService.getCartItem(skuId);
    model.addAttribute("cartItem", cartItemVo);
    return "success";
  }


  @RequestMapping("/checkCart")
  public String checkCart(@RequestParam("isChecked") Integer isChecked,@RequestParam("skuId")Long skuId) {
    cartService.checkCart(skuId, isChecked);
    return "redirect:http://cart.gulimall.com/cart.html";
  }

  @RequestMapping("/countItem")
  public String changeItemCount(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num) {
    cartService.changeItemCount(skuId, num);
    return "redirect:http://cart.gulimall.com/cart.html";
  }

  @RequestMapping("/deleteItem")
  public String deleteItem(@RequestParam("skuId") Long skuId) {
    cartService.deleteItem(skuId);
    return "redirect:http://cart.gulimall.com/cart.html";
  }

  @ResponseBody
  @RequestMapping("/getCheckedItems")
  public List<CartItemVo> getCheckedItems() {
    return cartService.getCheckedItems();
  }
}
